package com.example.admin.yuejian_22.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.admin.yuejian_22.Bean.UserBean;
import com.example.admin.yuejian_22.Listener.UserListener;
import com.example.admin.yuejian_22.Model.UserModel;
import com.example.admin.yuejian_22.R;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener,UserListener{
    private Button loginButton;
    private Button registButton;
    private EditText text_username;
    private EditText text_password;
    private UserBean loginResult;
    private Switch sw_remember;
    SharedPreferences sp;
    private LoginActivity loginActivity = this;
    String uname;
    String pwd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        initEvents();
        loginButton.setOnClickListener(this);
    }
    public void initViews(){
        loginButton=(Button)findViewById(R.id.loginbutton);
        registButton=(Button)findViewById(R.id.registbutton);
        text_username=(EditText)findViewById(R.id.text_username);
        text_password=(EditText)findViewById(R.id.text_password);
        sw_remember = (Switch)findViewById(R.id.sw);
        sp=getSharedPreferences("loginResult",MODE_PRIVATE);
        readSP();
    }
    private void readSP() {
        String uname = sp.getString("username",null);
        String pswd = sp.getString("password",null);
        Boolean remember = sp.getBoolean("remember",false);
        if (remember){
            text_username.setText(uname);
            text_password.setText(pswd);
            sw_remember.setChecked(remember);
        }
    }
    public void initEvents(){
        registButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, RegistActivity.class);
                startActivity(intent);
            }
        });
        loginButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if (text_username.getText().toString().trim().equals("")||text_password.getText().toString().trim().equals("")) {
                    Toast.makeText(LoginActivity.this, "用户名和密码不能为空",
                            Toast.LENGTH_SHORT).show();
                } else{
                    login();
                }
            }
        });

    } private void login() {
        UserModel userModel = new UserModel();
        userModel.Login(text_username.getText().toString().trim(), text_password.getText().toString().trim(),this);
    }


        @Override
    public void onClick(View v) {
            switch (v.getId()) {
                case R.id.loginbutton:
                    uname = text_username.getText().toString();
                    pwd = text_password.getText().toString();
                    saveSP();
                    loginResult = new UserBean();
                    UserModel userModel = new UserModel();
                    userModel.Login(uname, pwd, this);
                    break;
            }

    }
    private void saveSP() {
        SharedPreferences.Editor editor = sp.edit();
        String uname = text_username.getText().toString();
        editor.putString("username",uname);
        String pwd = text_password.getText().toString();
        editor.putString("password",pwd);
        Boolean remember = sw_remember.isChecked();
        editor.putBoolean("remember",remember);
        editor.commit();
    }
    @Override
    public void onResponse(Object beanobject) {

        saveUser(((UserBean)beanobject).getId());
//        String userid = response.body().getId().trim();
//        Log.v("获取id",userid);
//        saveUser(userid);
        Toast.makeText(this, "登录成功!", Toast.LENGTH_SHORT).show();
        Intent intent=new Intent(LoginActivity.this, HomeActivity.class);
        startActivity(intent);
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(this, "fail"+msg, Toast.LENGTH_SHORT).show();

    }
    public void saveUser(String id) {
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器 editor.putString("username", username.getText().toString());
        editor.putString("user_id", id);
        editor.putString("userpass", text_password.getText().toString());
        editor.commit();//提交修改
    }

//    private Button loginButton;
//    private Button registButton;
//    private EditText text_username;
//    private EditText text_password;
//    private UserBean userBean;
//    private Switch sw;
//    SharedPreferences sp;
//    private LoginActivity loginActivity = this;
//
//
//    UserListener userListener = new UserListener() {
//        @Override
//        public void onResponse(Object beanobject) {
//            Toast.makeText(loginActivity, "登录成功!", Toast.LENGTH_SHORT).show();
//            saveUser(((UserBean)beanobject).getId());
//            Intent intent=new Intent(LoginActivity.this, HomeActivity.class);
//            startActivity(intent);
//        }
//
//        @Override
//        public void onFail(String msg) {
//            Toast.makeText(loginActivity, "" + msg, Toast.LENGTH_SHORT).show();
//        }
//    };
//
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_login);
//        initViews();
//        initEvents();
//    }
//
//
//    public void initViews(){
//        loginButton=(Button)findViewById(R.id.loginbutton);
//        registButton=(Button)findViewById(R.id.registbutton);
//        text_username=(EditText)findViewById(R.id.text_username);
//        text_password=(EditText)findViewById(R.id.text_password);
//        sw = (Switch)findViewById(R.id.sw);
//        sp=getSharedPreferences("userBean",MODE_PRIVATE);
//        readSP();
//
//    }
//
//
//    private void readSP() {
//        String uname = sp.getString("username", null);
//        String pswd = sp.getString("password", null);
//        Boolean remember = sp.getBoolean("remember", false);
//        if (remember) {
//            text_username.setText(uname);
//            text_password.setText(pswd);
//            sw.setChecked(remember);
//        }
//    }
//
//
//    public void initEvents() {
//
//        registButton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent();
//                intent.setClass(LoginActivity.this, RegistActivity.class);
//                startActivity(intent);
//            }
//        });
//
//
//        loginButton.setOnClickListener(new View.OnClickListener(){
//            @Override
//            public void onClick(View v) {
//                if (text_username.getText().toString().trim().equals("")||text_password.getText().toString().trim().equals("")) {
//                    Toast.makeText(LoginActivity.this, "用户名和密码不能为空",
//                            Toast.LENGTH_SHORT).show();
//                } else{
//                    UserLogin();
//                }
//            }
//        });
//    }
//
//
//
//    private void UserLogin(){
//        UserModel userModel = new UserModel();
//        userModel.getLogin(text_username.getText().toString().trim(), text_password.getText().toString().trim(),userListener);
//    }
//
//
//    private void saveSP() {
//        SharedPreferences.Editor editor = sp.edit();
//        String uname = text_username.getText().toString();
//        editor.putString("username",uname);
//        String pwd = text_password.getText().toString();
//        editor.putString("password",pwd);
//        Boolean remember = sw.isChecked();
//        editor.putBoolean("remember",remember);
//        editor.commit();
//    }
//
//
//    public void saveUser(String id) {
//        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",
//                Context.MODE_PRIVATE);
//        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器 editor.putString("username", username.getText().toString());
//        editor.putString("user_id", id);
//        editor.putString("userpass", text_password.getText().toString());
//        editor.commit();//提交修改
//    }

}





